///////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2009. Dimarik
//
///////////////////////////////////////////////////////////////////////////////

//this._uri = new URI('gfx/images/jungle.png');
//this._uri = new URI('http://www.bandalismo.net/IMG/distant/gif/3408_1gif-040438.gif');
//this._uri = new URI('http://simoncpage.co.uk/blog/wp-content/uploads/2008/12/christmas-pixel-art-1.jpg');
//this._uri = new URI('http://s2.imgimg.de/uploads0/120790870511438bb0378png.png');
//this._uri = new URI('http://blog.turbomilk.com/images/pixel-office-large.gif');
//this._uri = new URI('http://www.dpk.com.ua/files/u4/pixel_city_part_2.png');
//this._uri = new URI('gfx/images/character/smb_mario_sheet.png');
//this._uri = new URI('gfx/images/character/mario_run.png');
//this._uri = new URI('http://www.gifandgif.eu/animated_gif/Arrows/Animated%20Gif%20Arrows%20(104).gif');
//this._uri = new URI('gfx/images/character/10-4.gif');

package
{

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Shape;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Matrix;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;

import ru.goodcore.core.GoodCore;
import ru.goodcore.utils.getDefinitionNames;

// import flash.display.Graphic
/**
 *  @author				Dimarik
 *  @version			1.1
 *  @langversion		3.0
 *  @playerversion		9.0
 *
 */
[SWF(width="600", height="600", frameRate="12", backgroundColor="#3A4044")]

public class ImageLoading extends GoodCore {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	public function ImageLoading() {
		super();
		super.stage.scaleMode = StageScaleMode.NO_SCALE;
		super.stage.align = StageAlign.TOP_LEFT;
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	/**
	 * @private 
	 */
	private var _rdStorage:		Array;

	/**
	 * @private 
	 */
	private var _index:			uint = 0;

	/**
	 * @privat 
	 */	
	private var _bitmap:		Bitmap;

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	public override function creationComplete():void {
		//var url:String = 'gfx/images/jungle.png';
		var url:String = 'two_frames_mc.swf';
		var request:	URLRequest = new URLRequest(url);

		if (url.indexOf('.swf') != -1) {
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			urlLoader.addEventListener(Event.COMPLETE, this.handler_complete);
			urlLoader.load(request);
		} else {
			var loader:		Loader = new Loader();
			var appDomain:	ApplicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
			var context:	LoaderContext = new LoaderContext(true,  appDomain);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.handler_complete);
			loader.load(request, context);
		}
	}

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Events handlers
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 */	
	private function handler_complete(event:Event):void {
		var target:Object = event.target;

		if (target is LoaderInfo) {
			var loaderInfo:LoaderInfo = LoaderInfo(event.target);
			loaderInfo.removeEventListener(Event.COMPLETE, this.handler_complete);

			var loader:Loader = LoaderInfo(event.target).loader;
			//var interactiveObject:	DisplayObject = super.addChild(loader);

			//trace('before: ', loader.numChildren);
			//interactiveObject = super.addChild(loader.content);

			//trace('after: ', loader.numChildren);

			//interactiveObject.addEventListener(
			//	MouseEvent.CLICK, 
			//	function(event:Event):void {
			//		trace('clicked');
			//	}
			//);
			
			this._bitmap = loader.content as Bitmap;
			var bitmapData:BitmapData = this._bitmap.bitmapData;
			
			//super.addChild(this._bitmap);
			
			var shape:Shape = new Shape();
			
			var g:Graphics = shape.graphics;
			//g.lineStyle(1, 0xFFFFFF);
			
			var q:Number = Math.PI;
			
			var matrix:Matrix = new Matrix(Math.cos(q), Math.sin(q), -Math.sin(q), Math.cos(q));
			//matrix = null;
			
			g.beginBitmapFill(bitmapData, matrix, true, true);
			g.drawRect(0, 0, 640, 360);
			g.endFill();
			super.addChild(shape);
			
			//shape.opaqueBackground = 0xFF0000;
			//shape.opaqueBackground = true;
			
			

			//var c:Class = loaderInfo.applicationDomain.getDefinition('D.workspace.GoodCore_game.bindebug.gfx.test_mc::gif') as Class;
			//var mc:MovieClip = new c() as MovieClip;
			//super.addChild(mc);

			//trace( getDefinitionNames(loaderInfo.bytes, true, false) );
		} else if (target is URLLoader) {
			trace( getDefinitionNames( (target as URLLoader).data, true, false) );
		}
	}

}
}